﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_AddTask]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_AddTask];
GO
CREATE PROCEDURE [dbo].[sproc_AddTask]
(
    @ArrangedBy         nvarchar(20),
    @Subject               nvarchar(50),
    @Detail               nvarchar(300),
    @ProjectID             int,
    @StartTime             datetime,
    @EndTime              datetime,
    @Attribute               int,
    @Type                   int,
    @Status                  int,
    @Tag                     int,
    @IsAwake              bit,
    @AwakeTime        datetime,
    @ContractList        nvarchar(50),
    @CooperatorList    nvarchar(200),
    @InsertedTaskID  [int] OUTPUT
 )
AS
    -- 不显示count信息
    SET NOCOUNT ON
    DECLARE @CurrentError int
    
    --  事务开始
    BEGIN TRANSACTION

    INSERT INTO UDS_Task(ArrangedBy,Subject,Detail,ProjectID,StartTime,EndTime,Attribute,Status,Tag,IsAwake,AwakeTime,ContractList,CooperatorList,Type)
    VALUES (@ArrangedBy, @Subject,@Detail,@ProjectID,@StartTime,@EndTime,@Attribute,@Status,@Tag,@IsAwake,@AwakeTime,@ContractList,@CooperatorList,@Type)
    select @InsertedTaskID=@@identity
   
    
    select @CurrentError = @@Error
    IF @CurrentError != 0
        BEGIN
               GOTO ERROR_HANDLER
        END

  
-- 事务结束
    COMMIT TRANSACTION
    -- 重设Count
    SET NOCOUNT OFF
    -- 成功的话返回0
    RETURN 0
  ERROR_HANDLER:
        ROLLBACK TRANSACTION
        SET NOCOUNT OFF
        RETURN @CurrentError